    $(document).ready(function() {
    	var whereClause={"all":"all"};
    	$.ajax({
    		url:'http://localhost:8080/easyskool/rest/masterservice/getMasterClass',
    		type:'POST',
    		data: '{"user":"user","whereClause":'+null+'}',
    		dataType: 'json',
    		contentType: "application/json; charset=utf-8",
    		success:function (data,testStatus,response) {
    			var json =JSON.parse(response.responseText);// $.parseJSON(response.responseText);
    			$.each(json, function(i, item) {
    			
    			$("#mytable").append("<tr id = " + i + ">" +
						 "<td class='class_id'>" + item.classId	+ "</td>" +
   					 "<td class='class_name'>" + item.className + "</td>" +
   					 "<td class='class_description'>"	+ item.classDesc + "</td>" +
   					 "<td class='created_by'>" + item.createdBy + "</td>" +
   					 "<td class='created_date'>" + item.createdDate + "</td>" +
   					 "<td class='modified_by'>" + item.modifiedBy	+ "</td>" +
   					 "<td class= 'modified_date'>" + item.modifiedDate	+ "</td>" +
   					 "<td><input type = 'button' value = 'Update' class  = 'btn btn-info' id ='update' data-toggle = 'modal' data-target='#myModal' /></td>" +
   					 "<td><input type = 'button' value = 'Delete' class  = 'btn btn-danger' id = 'remove'  /></td></tr>");

	});
    			},
    		error:function(data,testStatus,jqXHR){
    			alert("Bad thing happend! " + jqXHR+data.responseText+" "+testStatus);
    		}
    	});
    	$('#updateMasterClass').validate({
            rules: {
                classId : {
                    required: true
                },
                className: {
                    required: true
                }
            },
            highlight: function(element) {
                $(element).closest('.form-group').addClass('has-error');
            },
            unhighlight: function(element) {
                $(element).closest('.form-group').removeClass('has-error');
            },
            errorElement: 'span',
            errorClass: 'help-block',
            errorPlacement: function(error, element) {
                if(element.parent('.input-group').length) {
                    error.insertAfter(element.parent());
                } else {
                    error.insertAfter(element);
                }
            }
        });
    });	
    
    $('#mytable').on('click', '#remove', function(){
    	 var curRowId = $(this).closest('tr').attr("id");
    	 var map ="";
    	 map = map+ 	'{ "classId" 	: "' + $('#' + curRowId + ' td.class_id').text() +'",';
    	 map = map	+ 	'"className" 	: "' + $('#' + curRowId + ' td.class_name').text() 			+'",';
         map = map	+ 	'"classDesc" 	: "' + $('#' + curRowId + ' td.class_description').text() 	+'",';
         map = map 	+ 	'"createdBy" 	: "' + $('#' + curRowId + ' td.created_by').text() 			+'",';
         map = map 	+	'"createdDate" 	: "' + $('#' + curRowId + ' td.created_date').text() 		+'",';
         map = map	+ 	'"modifiedBy" 	: "' + $('#' + curRowId + ' td.modified_by').text() 		+'",' ;
         map = map 	+	'"modifiedDate"	: "' + $('#' + curRowId + ' td.modified_date').text() 		+'"}}';
    	alert(map);
    	var row = $(this).closest('tr');
        row.remove();
        				$.ajax({
        					url : 'http://localhost:8080/easyskool/rest/masterservice/deleteMasterClass',
    						type : 'POST',
    						data : '{"user":"aa","whereClause" :' +map,
    						dataType : 'json',
    						contentType : "application/json; charset=utf-8",
    						success : function(data,testStatus,jqXHR) {
    							alert(jqXHR.responseText);
    							window.location = "./easyskool/WebPages/Admin/AdminHome.jsp";
    						},
    						error : function(data,testStatus,jqXHR) {
    							alert("Error: ! "+ jqXHR+ data.responseText+ " "+ testStatus);
    						}
        					});
        
    });
    $('.form-fields').on('click', '#update', function(){
    	 var curRowId = $(this).closest('tr').attr("id");
         $('#classId').val( $('#' + curRowId + ' td.class_id').text() );
         $('#classId').data('abc',$('#' + curRowId + ' td.class_id').text());
         $('#className').val( $('#' + curRowId + ' td.class_name').text() );
         $('#classDesc').val( $('#' + curRowId + ' td.class_description').text() );
         $('#createdBy').val( $('#' + curRowId + ' td.created_by').text() );
         $('#createdDate').val( $('#' + curRowId + ' td.created_date').text() );
         $('#modifiedBy').val( $('#' + curRowId + ' td.modified_by').text() );
         $('#modifiedDate').val( $('#' + curRowId + ' td.modified_date').text() );
         });
    
    $('#updateMasterClass').on('click', '#update-db', function(){
    	if($('#updateMasterClass').valid()){
    	hiddenData = ($('#classId').data('abc'));
    	alert(hiddenData);
    			formData = ($('#updateMasterClass').serializeArray());
    				alert(formData);
    				$.ajax({
    					url : 'http://localhost:8080/easyskool/rest/masterservice/updateMasterClass',
						type : 'POST',
						data : '{"user":"'+ $("#aaCode").val()+ '","whereClause" : {"classId" : '+hiddenData+'},"updateClause":'+ converter(formData)+ '}',
						dataType : 'json',
						contentType : "application/json; charset=utf-8",
						success : function(data,testStatus,jqXHR) {
							alert(jqXHR.responseText);
							window.location = "./easyskool/WebPages/Admin/AdminHome.jsp";
						},
						error : function(data,testStatus,jqXHR) {
							alert("Error: ! "+ jqXHR+ data.responseText+ " "+ testStatus);
						}
    					});
    	}
    });
    
    
